package com.zb.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zb.dao.BaseDao;
import com.zb.dao.PartsDao;
import com.zb.entity.Msi;
import com.zb.entity.Parts;

public class PartsDaoImpl implements PartsDao{
	private BaseDao db=new BaseDao();
	public int getCount(String name, int msi) {
		String sql="SELECT COUNT(*) FROM msi m,parts p WHERE m.`id`=p.`type`";
		if(name!=null&&!"".equals(name.trim())) {
			sql+=" AND p.`bland` LIKE '%" + name + "%'";
		}
		if(msi>0) {
			sql+=" AND m.`id`="+msi;
		}
		ResultSet rs=db.query(sql);
		int count=1;
		try {
			if(rs.next()) {
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		
		return count;
	}

	public List<Parts> getMsiPage(int index, int size, String name, int msi) {
		List<Parts>list=new ArrayList<Parts>();
		String sql="SELECT p.*,m.`typename` FROM msi m,parts p WHERE m.`id`=p.`type`";
		if(name!=null&&!"".equals(name.trim())) {
			sql+=" AND p.`bland` LIKE '%" + name + "%'";
		}
		if(msi>0) {
			sql+=" AND m.`id`="+msi;
		}
		sql+=" LIMIT ?,?";
		ResultSet rs=db.query(sql,(index - 1) * size, size);
		try {
			while(rs.next()) {
				Parts p=new Parts();
				p.setId(rs.getInt(1));
				p.setBland(rs.getString(3));
				p.setPrice(rs.getDouble(4));
				p.setImg(rs.getString(5));
				Msi m=new Msi();
				m.setId(rs.getInt(2));
				m.setTypeName(rs.getString(6));
				p.setMsi(m);
				list.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return list;
	}

}
